package service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import database.*;
import entity.*;

/**
 *
 * @author Trung
 */
public class TypeRoom {
        Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblroom";
	
	public boolean AddTypeRoom(String name, int charge, String info){
		connect = Conn.getConnect();
		if (connect == null)
			System.out.println("null");
		try{
			pstm= connect.prepareStatement("INSERT INTO "+ table+"(namecharge,info) VALUES (?,?,?)");
			pstm.setString(1,name);
			pstm.setInt(2, charge);
			pstm.setString(3, info);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
	}
	
	public boolean DeleteRoom(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateRoom(int id, String name,int charge,String info){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET name=?," +
					"charge=?,info=?  WHERE id=?");
			pstm.setString(1, name);
			pstm.setInt(2, charge);
			pstm.setString(3,info);
                        pstm.setInt(4,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public  ArrayList<entity.TypeRoom> GetAllRoom(){
		connect= Conn.getConnect();
		ArrayList<entity.TypeRoom> list = new ArrayList<entity.TypeRoom>();		
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);			
			rs=pstm.executeQuery();			
			while(rs.next()){
				entity.TypeRoom room = new entity.TypeRoom();
				room.setId(rs.getInt("id"));
				room.setName(rs.getString("name"));
                                room.setCharge(rs.getInt("charge"));
				room.setInfo(rs.getString("info"));
				list.add(room);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public entity.TypeRoom GetTypeRoomById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		entity.TypeRoom room = null;
		ArrayList<entity.TypeRoom> list = new ArrayList<entity.TypeRoom>();	
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
		while(rs.next()){
			room =  new entity.TypeRoom();;
			room.setId(rs.getInt("id"));
			room.setName(rs.getString("name"));
			room.setCharge(rs.getInt("charge"));
			room.setInfo(rs.getString("info"));
                        list.add(room);
		}
	
		}catch(SQLException e){
			e.printStackTrace();
		}
		return room;
	}
}
